package com.ecy.quadratic.ui.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ecy.quadratic.R;
import com.ecy.quadratic.adapter.CommunityCommentListViewAdapter;
import com.ecy.quadratic.beans.Comment;
import com.ecy.quadratic.beans.PageInfo;
import com.ecy.quadratic.beans.support.BookDetailEvent;
import com.ecy.quadratic.beans.user.User;
import com.ecy.quadratic.component.DaggerNovelMainComponent;
import com.ecy.quadratic.manager.LoginManager;
import com.ecy.quadratic.reader.base.BaseFragment;
import com.ecy.quadratic.reader.component.AppComponent;
import com.ecy.quadratic.ui.activity.LoginActivity;
import com.ecy.quadratic.ui.activity.PostCommentActivity;
import com.ecy.quadratic.ui.contract.CommentLikeContract;
import com.ecy.quadratic.ui.presenter.BookCommentPresenter;
import com.ecy.quadratic.widget.SwipeRefreshView;
import com.yuyh.easyadapter.glide.GlideCircleTransform;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;


/**
 * Created by Administrator on 2016/3/31.
 */
public class BookDescriptionFragment extends BaseFragment implements CommentLikeContract.View {

    private static final String BOOK_ID = "book_id";
    @Bind(R.id.comic_detail_swipe_refreshview)
    SwipeRefreshView mSwipeRefreshView;
    @Bind(R.id.comment_listview)
    ListView mCommentListView;
    TextView mPostCommentTV;
    TextView mDescriptionTV;
    ImageView mPortraitIV;
    CommunityCommentListViewAdapter mCommentAdapter;
    List<Comment> mList;
    @Inject
    BookCommentPresenter mPresenter;
    private int mCurrentPage = 0;
    private int mTotalPage = 0;
    private String bookId;
    private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            mPresenter.refresh(bookId, "1");
        }
    };
    private SwipeRefreshView.OnLoadListener mOnLoadListener = new SwipeRefreshView.OnLoadListener() {

        @Override
        public void onLoad() {
            mPresenter.loadMore(bookId, mCurrentPage + 1 + "");
        }
    };

    public static BookDescriptionFragment newInstance(String bookId) {
        BookDescriptionFragment fragment = new BookDescriptionFragment();
        Bundle bundle = new Bundle();
        bundle.putString(BOOK_ID, bookId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_comic_description;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerNovelMainComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void attachView() {
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null)
            mPresenter.detachView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void initData() {
        mList = new ArrayList<>();
        bookId = getArguments().getString(BOOK_ID);
        mCommentAdapter = new CommunityCommentListViewAdapter(mContext, mList);
    }

    @Override
    public void configViews() {
        mSwipeRefreshView.setProgressBackgroundColorSchemeResource(android.R.color.white);
        mSwipeRefreshView.setColorSchemeResources(R.color.hobby_theme_color, R.color.hobby_theme_color, R.color.hobby_theme_color);
        mSwipeRefreshView.setOnRefreshListener(mOnRefreshListener);
        mSwipeRefreshView.setOnLoadListener(mOnLoadListener);
        View header = LayoutInflater.from(getContext()).inflate(R.layout.comic_description_header, null);

        mPortraitIV = (ImageView) header.findViewById(R.id.header_portrait_iv);
        final User user = LoginManager.getInstance().getUser();
        if (user != null) {
            Glide.with(this).load(user.portrait).centerCrop().bitmapTransform(new GlideCircleTransform(getContext())).into(mPortraitIV);
        }
        mPostCommentTV = (TextView) header.findViewById(R.id.post_comment_tv);
        mPostCommentTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (user != null) {
                    PostCommentActivity.startActivity(getContext(), 2, bookId);
                } else {
                    LoginActivity.startActivity(getContext());
                }

            }
        });
        mDescriptionTV = (TextView) header.findViewById(R.id.comic_description_tv);
        mCommentListView.addHeaderView(header);
        mCommentListView.setAdapter(mCommentAdapter);
        mSwipeRefreshView.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshView.setRefreshing(true);
                mOnRefreshListener.onRefresh();
            }
        });
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void showDescription(BookDetailEvent event) {
        if (event.mBookId.equals(bookId))
            mDescriptionTV.setText(event.mBookDetail);
    }

    @Override
    public void showRefreshList(PageInfo<Comment> pageInfo) {

        if (pageInfo != null) {
            mCurrentPage = pageInfo.pageNum;
            mTotalPage = pageInfo.pages;
            mList.clear();
            mCommentAdapter.notifyDataSetChanged();
            mList.addAll(pageInfo.list);
            mCommentAdapter.notifyDataSetChanged();
//            if (mList.size()>0) {
//                mCommentListView.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        mCommentListView.smoothScrollToPosition(0);
//                    }
//                });
//            }
        }
    }

    @Override
    public void showLoadMoreList(PageInfo<Comment> pageInfo) {
        if (pageInfo != null && mCurrentPage != pageInfo.pageNum) {
            mCurrentPage = pageInfo.pageNum;
            mTotalPage = pageInfo.pages;
            mList.addAll(pageInfo.list);
            mCommentAdapter.notifyDataSetChanged();
        }
    }

//    @OnClick(R.id.tv_post_comment)
//    public void postComment() {
//        mPresenter.postComment(mCommunity.id,"", LoginManager.getInstance().getUserJson(),etPostComment.getText().toString());
//    }

    @Override
    public void postCommentSuccesful() {
//        etPostComment.setText("");
//        ToastUtils.getSingleToast("发表评论成功",Toast.LENGTH_SHORT).show();
//        onEditClick();
//        mSwipeRefreshView.setRefreshing(true);
//        mOnRefreshListener.onRefresh();
    }

    @Override
    public void postLikeSuccessful() {

    }

//    @OnClick(R.id.post_comic_comment)
//    public void postComic() {
//        PostCommentActivity.startActivity(getContext(),1,mComicId);
//    }

    @Override
    public void showError() {
        mSwipeRefreshView.setRefreshing(false);
        mSwipeRefreshView.setLoading(false, true);
    }

    @Override
    public void complete() {
        mSwipeRefreshView.setRefreshing(false);
        mSwipeRefreshView.setLoading(false);
    }
}


